#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QThread>
#include <QString>
#include "pcf8574.h"
#include "CH341DLL.H"

static bool m_leftMousePressed = false;
static QPoint m_StartPoint;
static unsigned long EventStatus;

PCF8574 *PCF8574::Stapcf8574 = nullptr;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    pcf8574 = new PCF8574(this);
    PCF8574::Stapcf8574 = new PCF8574(this);
//    PCF8574::Stapcf8574->EventStatus = 0;
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);  //设置窗口背景透明
    ui->ic_type->addItem("PCF8574");
    ui->ic_type->addItem("PCF8574A");
    QStringList strList;
    strList << "全选" << "000(U2)" << "001(U4)" << "010(U6)" << "011(U8)" << "100(U10)" << "101(U12)" << "110(U14)" << "111(U16)";
    ui->ic_number->addItems(strList);
    QStringList eeromList;
    eeromList << "24c01" << "24c02" << "24c08" << "24c16" << "24c32";
    ui->eerom_type->addItems(eeromList);
    connect(PCF8574::Stapcf8574, &PCF8574::Device_updata, this, &MainWindow::open_closeDevice);
    CH341SetDeviceNotify(0, nullptr, Device_status_update);
    CH341OpenDevice(0);
    CH341SetStream(0, 0x10);
}

MainWindow::~MainWindow()
{
    CH341CloseDevice(0);
    delete ui;
}



void _stdcall MainWindow::Device_status_update(unsigned long iEventStatus)
{
    EventStatus = iEventStatus;
    emit PCF8574::Stapcf8574->Device_updata();
}

void MainWindow::open_closeDevice()
{
    if(EventStatus == 0) {
        //设备已移除
        qDebug() << "移除";
        CH341CloseDevice(0);
        ui->logwindow->append(TEXT_COLOR_RED("设备移除"));
    } else if(EventStatus == 3) {
        //设备已插入
        qDebug() << "插入";
        ui->logwindow->append(TEXT_COLOR_RED("设备插入"));
        CH341OpenDevice(0);
        CH341SetStream(0, 0x10);
    }
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    //只能是鼠标左键移动和改变大小
    if (event->button() == Qt::LeftButton) {
        if (ui->widget->underMouse()) {
            m_leftMousePressed = true;
            //按下时鼠标左键时，窗口在屏幕中的坐标
            m_StartPoint = event->globalPos();
        }
    }
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    //移动窗口
    if (m_leftMousePressed) {
        QPoint curPoint = event->globalPos();   //按住移动时的位置
        QPoint movePoint = curPoint - m_StartPoint;
        //普通窗口
        QPoint mainWinPos = this->pos();
        this->move(mainWinPos.x() + movePoint.x(), mainWinPos.y() + movePoint.y());
        m_StartPoint = curPoint;
    }
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    event->pos();
    m_leftMousePressed = false;//释放鼠标
}

void MainWindow::on_eerom_type_activated(int index)
{
    qDebug() << index;
}

void MainWindow::on_openled_clicked()
{
    if(ui->ic_type->currentIndex() == 0) { //pcf8574
        switch (ui->ic_number->currentIndex()) {
            case 0: {
                    bool ack = 0;
                    ack = pcf8574->PCF8574_WriteIO(0, 0x41, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x43, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x45, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x47, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x49, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x4b, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x4d, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x4f, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 1: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x41, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 2: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x43, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 3: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x45, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 4: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x47, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 5: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x49, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 6: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x4b, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 7: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x4d, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 8: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x4f, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
        }
    }
    if(ui->ic_type->currentIndex() == 1) { //pcf8574A
        switch (ui->ic_number->currentIndex()) {
            case 0: {
                    bool ack = 0;
                    ack = pcf8574->PCF8574_WriteIO(0, 0x71, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x73, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x75, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x77, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x79, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x7b, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x7d, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x7f, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 1: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x71, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 2: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x73, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 3: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x75, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 4: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x77, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 5: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x79, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 6: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x7b, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 7: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x7d, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 8: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x7f, 0xff);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
        }
    }
}

void MainWindow::on_closeled_clicked()
{
    if(ui->ic_type->currentIndex() == 0) { //pcf8574
        switch (ui->ic_number->currentIndex()) {
            case 0: {
                    bool ack = 0;
                    ack = pcf8574->PCF8574_WriteIO(0, 0x41, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x43, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x45, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x47, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x49, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x4b, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x4d, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x4f, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 1: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x41, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 2: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x43, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 3: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x45, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 4: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x47, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 5: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x49, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 6: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x4b, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 7: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x4d, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 8: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x4f, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
        }
    }
    if(ui->ic_type->currentIndex() == 1) { //pcf8574A
        switch (ui->ic_number->currentIndex()) {
            case 0: {
                    bool ack = 0;
                    ack = pcf8574->PCF8574_WriteIO(0, 0x71, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x73, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x75, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x77, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x79, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x7b, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x7d, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    ack = pcf8574->PCF8574_WriteIO(0, 0x7f, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 1: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x71, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 2: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x73, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 3: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x75, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 4: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x77, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 5: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x79, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 6: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x7b, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 7: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x7d, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
            case 8: {
                    bool ack = pcf8574->PCF8574_WriteIO(0, 0x7f, 0x00);
                    if(ack == 0) {
                        ui->logwindow->append(TEXT_COLOR_REDACK(QString::number(ack), ui->ic_number->currentText()));
                    } else {
                        ui->logwindow->append(TEXT_COLOR_GREENACK(QString::number(ack), ui->ic_number->currentText()));
                    }
                    break;
                }
        }
    }
}

void MainWindow::on_ic_type_activated(int index)
{
    qDebug() <<  "on_ictype_activated" << index;
    pcf8574->indexic = index;
}

void MainWindow::on_ic_number_activated(int index)
{
    qDebug() << index;
    pcf8574->numberic = index;
}

void MainWindow::on_min_clicked_clicked()
{
    this->showMinimized();
}

void MainWindow::on_close_clicked_clicked()
{
    QWidget *pWindow = this->window();
    pWindow->close();
}

void MainWindow::on_deleteLog_clicked()
{
    ui->logwindow->clear();
}
